import 'dart:ui';
import 'package:get/get.dart';
import 'package:package_info_plus/package_info_plus.dart';
import '../index.dart';

/// 配置服务
class ConfigService extends GetxService {

  // 单例写法
  static ConfigService get to => Get.find();

  String? version = '';// app版本号
  String? deviceID = '';// 设备Id
  String? systemVersion = '';// 设备系统版本

  // app 生命周期
  AppLifecycleState _appLifecycle = AppLifecycleState.resumed;
  AppLifecycleState get appLifecycle => _appLifecycle;
  void setAppLifecycle(AppLifecycleState value) => _appLifecycle = value;

  @override
  void onInit() {
    super.onInit();
    getDeviceInfo();
  }

  @override
  void onReady() {
    super.onReady();
    initMap();
  }

  // 初始化平台信息(app首次同意协议之后允许执行)
  void getDeviceInfo({ValueChanged ? onSuccess}){
    if (Storage().getString(Constants.storageAppInit) != '1') return;
    Future.wait([
      getPlatform(),
      initDeviceID(),
      initSystemVersion(),
    ]).whenComplete(() {
      if(onSuccess != null) onSuccess(true);
    });
  }

  // 获取安装包信息
  Future<void> getPlatform() async {
    PackageInfo platform = await PackageInfo.fromPlatform();
    version = platform.version;
  }

  // 获取设备id信息
  Future<void> initDeviceID() async {
    deviceID = await DeviceUtils.deviceId();
  }

  // 获取设备系统版本信息
  Future<void> initSystemVersion() async {
    systemVersion = await DeviceUtils.systemVersion();
  }

  /// map相关隐私提前告知用户
  void initMap() async {
    // // 设置apikey
    // AMapFlutterLocation.setApiKey(
    //     Constants.aMapAnroidApiKey, Constants.aMapIosApiKey);
    // AMapFlutterLocation.updatePrivacyShow(true, true);
    // // 隐私权政策是否已经取得用户同意
    // AMapFlutterLocation.updatePrivacyAgree(true);
    // // 检查定位权限
    // var status = await Permission.location.status;
    // if (status.isGranted) {
    //   /// 拥有权限才开始定位
    //
    // }
  }
}
